package buttons.control;

import buttons.view.V_Button;
import controllers.C_TooltipControl;
import controllers.ControlManager;
import utilities.EventOwner;

public class C_Button extends EventOwner
{
  protected C_TooltipControl c_toolTip;
  protected String tooltipText = "";
  protected boolean wasPressed = false;
  protected String bID = "b1";
  protected boolean enabled = true;
  
  public C_Button(V_Button vButton)
  {
    vFeature = vButton;
  }
  
  @Override
  public void onInteract (float mouseXLoc, float mouseYLoc, String interID)
  {
      V_Button button = (V_Button)vFeature;
      boolean mouseOver = button.contains(mouseXLoc, mouseYLoc);
      if(mouseOver)
      {
          if(interID.equals("leftClick"))
          {
            triggerEvent("buttonPressed");
          }
          if(interID.equals("leftPressed"))
          {
            button.setOffset(-20);
            wasPressed = true;
          }
          else // if mouse over...
          {
              if (interID.equals("released") || interID.equals("")) {
                if (wasPressed) {
                  triggerEvent("buttonPressed"); // deal with slight movements of the mouse (user inprecision)
                  wasPressed = false;
                }
                // set the background to an "over" color
                button.setOffset(20);
                // process tooltip operations (but only if tooltips were enabled for this button)
                handleTooltip(mouseXLoc, mouseYLoc);
            }
          }          
      }
      else // if mouse not over the button
      { 
        wasPressed = false;
        button.setOffset(0);
      }
  }
  
  protected void handleTooltip(float mouseXLoc, float mouseYLoc)
  {
	  if(c_toolTip != null)
      {
          if(c_toolTip.checkForTip(mouseXLoc, mouseYLoc))
          {
              if(tooltipText.length() > 0) // if the button HAS a set tooltip display it
              {
                c_toolTip.setName(tooltipText);
                c_toolTip.setActive(true);
              }
          }
          else c_toolTip.setActive(false);
      } 
  }
  
  public void setTooltip(C_TooltipControl tooltipController, String tipText)
  {
	  c_toolTip = tooltipController;
	  tooltipText = tipText;
  }
  
  public String getTooltip()
  {
    return tooltipText;
  }
  
  public String getName()
  {
    return ((V_Button)vFeature).getName();
  }
  
  public String getBID()
  {
    return bID;
  }
  
  public void setBID(String val)
  {
    bID = val;
  }
  
  public void setEnabled(boolean val)
  {
	  enabled = val;
  }
}
